#!/usr/bin/env python
"""通过多进程处理同步速度过慢的问题"""
import os
import sys
import multiprocessing
import math
import requests

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'publicApi.settings')
from publicApi import settings

FLASK_SERVER_HOST = getattr(settings, 'FLASK_SERVER_HOST', 'http://127.0.0.1:5000/')


def syncMultiprocess(page=1, page_size=50, handCounts=200):
    os.system("python manage.py sync_weather --page %s --size %s --count %s" % (page, page_size, handCounts))


def getCounts(min_size=1, option='weather'):
    url = '%s%s?page=1&page_size=%s' % (FLASK_SERVER_HOST, option, min_size)
    response = requests.get(url)
    if response.status_code != 200:
        # logging
        return 0
    return response.json().get('counts', 0)


def main():
    counts = getCounts(1, 'weather')
    if counts > 0:
        page, page_size, processes = [1, 20, 6]
        pro_counts = math.ceil(counts / processes)  # 每个进程要处理的数据数 | 示例：427
        page_range = math.ceil(pro_counts / page_size)  # 每个进程包含的page范围 | 示例：22
        # 多进程同步
        pool = multiprocessing.Pool(processes=processes)
        for i in range(processes):
            pool.apply_async(syncMultiprocess, (page, page_size, pro_counts))
            page = page + page_range

        pool.close()
        pool.join()
        print("======> poolsync is finish !")


if __name__ == '__main__':
    main()
